package org.freez4.spring.ioc.overview.dependency.lookup;

import org.freez4.spring.ioc.overview.annotation.Super;
import org.freez4.spring.ioc.overview.domain.User;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.ListableBeanFactory;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * 根据Bean名称查找。
 * <p>
 * 1.实时查找demo
 *
 * @author 阿坤同学
 * @date 2023/11/30 22:49
 */
public class DependencyLookupDemo {
    private static final Logger logger = Logger.getLogger(DependencyLookupDemo.class.getName());

    public static void main(String[] args) {
        BeanFactory beanFactory = new ClassPathXmlApplicationContext("classpath:/META-INF/dependency-lookup-context.xml");
        lookupInRealTime(beanFactory);
        lookupInLazy(beanFactory);
        lookupByType(beanFactory);
        lookupCollectionByType(beanFactory);
        lookupByNameAndType(beanFactory);
        lookupByAnnotationCollection(beanFactory);
    }

    /**
     * 通过@Spuer注解实时查找用户信息。
     *
     * @param beanFactory ioc容器
     * @author 阿坤同学
     * @date 2023/11/30 23:54
     */
    public static void lookupByAnnotationCollection(BeanFactory beanFactory) {
        if (beanFactory instanceof ListableBeanFactory) {
            ListableBeanFactory listenerBeanFactory = (ListableBeanFactory) beanFactory;
            Map<String, User> users = (Map) listenerBeanFactory.getBeansWithAnnotation(Super.class);
            logger.log(Level.INFO, () -> "通过@Spuer注解实时查找用户信息: " + users);
        }
    }

    /**
     * 通过bean名称和类型实时查找用户信息。
     *
     * @param beanFactory ioc容器
     * @author 阿坤同学
     * @date 2023/11/30 23:41
     */
    private static void lookupByNameAndType(BeanFactory beanFactory) {
        User user = beanFactory.getBean("user", User.class);
        logger.log(Level.INFO, () -> "通过bean名称和类型实时查找用户信息: " + user);
    }

    /**
     * 通过集合bean类型实时查找用户信息。
     *
     * @param beanFactory ioc容器
     * @author 阿坤同学
     * @date 2023/11/30 23:43
     */
    private static void lookupCollectionByType(BeanFactory beanFactory) {
        if (beanFactory instanceof ListableBeanFactory) {
            ListableBeanFactory listableBeanFactory = (ListableBeanFactory) beanFactory;
            Map<String, User> users = listableBeanFactory.getBeansOfType(User.class);
            logger.log(Level.INFO, () -> "通过集合bean类型实时查找用户信息: " + users);
        }
    }

    /**
     * 通过bean类型实时查找用户信息。
     *
     * @param beanFactory ioc容器
     * @author 阿坤同学
     * @date 2023/11/30 23:42
     */
    private static void lookupByType(BeanFactory beanFactory) {
        User user = beanFactory.getBean(User.class);
        logger.log(Level.INFO, () -> "通过bean类型实时查找用户信息: " + user);
    }

    /**
     * 通过bean名称延迟查找用户信息。
     *
     * @param beanFactory ioc容器
     * @author 阿坤同学
     * @date 2023/11/30 23:25
     */
    private static void lookupInLazy(BeanFactory beanFactory) {
        ObjectFactory<User> objectFactory = (ObjectFactory<User>) beanFactory.getBean("objectFactory");
        logger.log(Level.INFO, () -> "通过bean名称延迟查找用户信息: " + objectFactory.getObject());
    }

    /**
     * 通过bean名称实时查找用户信息。
     *
     * @param beanFactory ioc容器
     * @author 阿坤同学
     * @date 2023/11/30 23:16
     */
    private static void lookupInRealTime(BeanFactory beanFactory) {
        User user = (User) beanFactory.getBean("user");
        logger.log(Level.INFO, () -> "通过bean名称实时查找用户信息: " + user);
    }

}
